#include "playvideowidget.h"
#include "ui_playvideowidget.h"
#include <QPainter>
#include <QDebug>

PlayVideoWidget::PlayVideoWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::PlayVideoWidget)
{
    ui->setupUi(this);
    connect(mDecodeThread,SIGNAL(sendQImage(QImage)),this,SLOT(receiveQImage(QImage)));
    connect(mDecodeThread,&HwdDecodeThread::finished,mDecodeThread,&HwdDecodeThread::deleteLater);
}

PlayVideoWidget::~PlayVideoWidget()
{
    qDebug()<<"exit player";
    if(mDecodeThread->isRunning()){
        stop();
    }
    delete ui;
}

void PlayVideoWidget::set_url(QString url)
{
    mDecodeThread->set_path(url);
}

void PlayVideoWidget::play()
{
    stop();
    mDecodeThread->start();
}

void PlayVideoWidget::stop()
{
    if(mDecodeThread->isRunning()){
        //线程里是自定义的死循环，通过quit()和exit()函数停止不了。
//        mDecodeThread->requestInterruption();
//        mDecodeThread->quit();
//        mDecodeThread->wait(100);

        //自定义的运行停止函数
        mDecodeThread->stop_thread();
    }
    mDecodeThread->ffmpeg_free_variables();
}

void PlayVideoWidget::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    painter.drawImage(0,0,img);
}

void PlayVideoWidget::slot_receive_image(const QImage &rImg)
{
    img = rImg.scaled(this->size());
    update();
}
